<?php
class SettingController extends BaseAdminController
{
	public function actionIndex()
	{
		$file_config = Yii::app()->getBasePath().'/config/config.txt';
		if(isset($_POST['Setting'])){
			$configtext = '';
			foreach ($_POST['Setting'] as $key => $value){
				$configtext .= $key.'{:}'.$value."\n";
			}
			$fh = fopen($file_config, 'w') or die("can't open file");
			fwrite($fh, $configtext);
			fclose($fh);
			Yii::app()->user->setFlash('msg','Setting saved successful!');
		}
		$data = Common::ReadConfig();
		$this->render('index', array('data'=>$data));
	}
	public function actionMail()
	{
		$file_config = Yii::app()->getBasePath().'/config/config.txt';
		if(isset($_POST['Setting'])){
			$configtext = '';
			foreach ($_POST['Setting'] as $key => $value){
				$configtext .= $key.'{:}'.$value."\n";
			}
			$fh = fopen($file_config, 'w') or die("can't open file");
			fwrite($fh, $configtext);
			fclose($fh);
			Yii::app()->user->setFlash('msg','Setting saved successful!');
		}
		$data = Common::ReadConfig();
		$this->render('mail', array('data'=>$data));
	}
	public function actionCache()
	{
		$cache_path = Yii::app()->getBasePath().'/runtime/cache/';
		$expired = array();
		$all_cached = array();
		if($handle=opendir($cache_path)){
            while (false !== ($item = readdir($handle))){
                if ($item != "." && $item != ".." ){
                    $cacheFile = $cache_path . DIRECTORY_SEPARATOR . $item;
                    if (is_file($cacheFile)){
						$expire = filemtime($cacheFile);
                        if($expire > 0 && $expire < time())
                          $expired[] = $cacheFile;
                        $all_cached[] = $cacheFile;
                    }
                }
            }
            closedir($handle);
        }
        if($_POST['Cache']){
        	$flag = empty($_POST['Cache']['flag'])?0:1;
        	//clear
        	if($flag === 0){
        		//expired clear
        		foreach ($expired as $value){
        			@unlink($value);
        		}
        	}elseif($flag === 1){
        		foreach ($all_cached as $value){
        			@unlink($value);
        		}
        	}
        	$this->refresh();
        }
		$this->render('cache', array('expired'=>$expired, 'all_cached'=>$all_cached));
	}
}